<?php

namespace App\Jobs;

use App\Admin\Repositories\MessageReceiver;
use App\Models\Message;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class SendMessage implements ShouldQueue {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $message;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Message $message) {
        $this->message = $message;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle() {
        $user_list = [];
        switch ($this->message->receiver_type) {
        case 1: //普通用户
            $user_list = DB::table('users')->select('id as user_id')->get()->toArray();
            break;
        case 2: //司机
            $user_list = DB::table('drivers')->where('status', '2')->select('user_id')->get()->toArray();
            break;
        case 3: //管理管理员
            $user_list = DB::table('management')->select('id as user_id')->get()->toArray();
            break;
        default:
            break;
        }
        $message_model = new MessageReceiver();
        foreach ($user_list as $user_info) {
            $message_model->addReceiver($this->message->id, $user_info['user_id']);
        }
    }
}
